package algorithm;

public class leetcode_1143 {
    public int longestCommonSubsequence(String text1, String text2) {
        int len1=text1.length();
        int len2=text2.length();
        char[] list1=text1.toCharArray();
        char[] list2=text2.toCharArray();
        int [][] dp=new int[len1+1][len2+1];
        for(int i=1;i<len1+1;++i){
            for(int j=1;j<len2+1;++j){
                if(list1[i-1]==list2[j-1]){
                    dp[i][j]=dp[i-1][j-1]+1;
                }
                else{
                    dp[i][j]=max(dp[i-1][j-1],dp[i][j-1],dp[i-1][j]);
                }
            }
        }
        return dp[len1][len2];
    }
    public int max(int a,int b,int c){
        if(a<b){
            return (b>c)?b:c;
        }
        else{
            return (a>c)?a:c;
        }
    }
}
